import {
  getAppointmentShopDetail,
  getOrderDetail,
  shareGoodOrder,
} from "@src/api/reservation";
import { RESERVATION_PAY_TYPE } from "@src/sp-reservation/constant/index.js";
import { getWxaMsgTpl } from "@src/api/base";
import wxUtils from "@src/common/wxUtils";
import router from "@src/router";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import reservation from "@src/store/reservation";
import location from "@src/store/location";
import base from "@src/store/base";
Page({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ["isiPhoneX", "themeColor", "themeTitleColor"],
    },
  ],
  data: {
    //路由信息
    options: {},
    //order信息
    orderDetail: {},
    //店铺信息
    shopInfo: {},
    isShowShareGood: false,
    isShowShareLink: false,
    isNotice: true,
    RESERVATION_PAY_TYPE,
  },
  onShareAppMessage: function () {
    let fileBaseUrl = getApp().globalData.extConfig.fileBaseUrl;
    const {
      shopInfo: {
        shopInfoResp: { shopId, shopConfigId },
      },
      orderDetail: { goodsVOList, appointmentPlanId },
    } = this.data;
    console.log(goodsVOList[0].coverImg);
    return {
      title: goodsVOList[0].title,
      imageUrl: fileBaseUrl + goodsVOList[0].coverImg,
      path: `/sp-reservation/pages/project_detail/main?planId=${appointmentPlanId}&shopId=${shopId}&shopConfigId=${shopConfigId}&goodsSerial=${goodsVOList[0].goodsSerial}`,
    };
    // return custom share data when user share.
  },
  onShareTimeline: function () {
    let fileBaseUrl = getApp().globalData.extConfig.fileBaseUrl;
    const {
      shopInfo: {
        shopInfoResp: { shopId, shopConfigId },
      },
      orderDetail: { goodsVOList, appointmentPlanId },
    } = this.data;
    return {
      title: goodsVOList[0].title,
      imageUrl: fileBaseUrl + goodsVOList[0].coverImg,
      path: `/sp-reservation/pages/project_detail/main?planId=${appointmentPlanId}&shopId=${shopId}&shopConfigId=${shopConfigId}&goodsSerial=${goodsVOList[0].goodsSerial}`,
    };
    // return custom share data when user share.
  },
  onLoad: function (options) {
    this.setData({
      options,
    });
    this.fetchData();
  },
  async fetchData() {
    const {
      options: { orderNo, shopId },
    } = this.data;
    //获取门店信息
    const { code, data } = await getAppointmentShopDetail({ shopId });
    const { code: orderCode, data: orderData } = await getOrderDetail({
      orderNo,
      shopId,
    });
    if (code === 0) {
      this.setData({
        shopInfo: data,
      });
    }
    if (orderCode === 0) {
      this.setData({
        orderDetail: orderData,
      });
    }
  },
  handleShare() {
    this.setData({
      isShowShareLink: true,
    });
  },
  handleShowShareLink() {
    this.setData({
      isShowShareLink: false,
    });
  },
  onShowShareGood() {
    this.setData({
      isShowShareLink: false,
      isShowShareGood: true,
    });
  },
  handleCloseShare() {
    this.setData({
      isShowShareGood: false,
    });
  },
  handleToDetail() {
    router.redirectTo({
      name: "reservation/reservation_order_detail",
      params: {
        orderNo: this.data.orderDetail.orderNo,
        shopId: this.data.shopInfo.shopInfoResp.shopId,
      },
    });
  },
  handleAddNotice() {
    getWxaMsgTpl({
      bizCode: "APPOINTMENT_PLACE_ORDER",
    }).then((res) => {
      wxUtils.requestSubscribeMessage(res.data).then((result) => {
        if (result[res.data[0]] === "accept") {
          wx.showToast({
            title: "订阅提醒成功",
            icon: "success",
            duration: 1500,
            success: () => {
              this.setData({
                isNotice: false,
              });
            },
          });
        }
      });
    });
  },
});
